package com.freecloud.whispered.auth.repository;

import com.freecloud.framework.repository.BaseDao;
import com.freecloud.whispered.auth.entity.SysUserEntity;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * 系统用户
 * 
 * @author maomao
 * @email 138383877@qq.com
 */
@Repository
public interface SysUserDao extends BaseDao<SysUserEntity,Long> {
	
	/**
	 * 查询用户的所有权限
	 * @param userId  用户ID
	 */
	@Query(value = "select m.perms from sys_user_role ur \n" +
			"\t\t\tLEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id \n" +
			"\t\t\tLEFT JOIN sys_menu m on rm.menu_id = m.menu_id \n" +
			"\t\twhere ur.user_id = :userId",nativeQuery = true)
	List<String> queryAllPerms(@Param("userId") Long userId);
	
	/**
	 * 查询用户的所有菜单ID
	 */
	@Query(value = "select distinct rm.menu_id from sys_user_role ur \n" +
			"\t\t\tLEFT JOIN sys_role_menu rm on ur.role_id = rm.role_id \n" +
			"\t\twhere ur.user_id = :userId",nativeQuery = true)
	List<Long> queryAllMenuId(@Param("userId") Long userId);
	
	/**
	 * 根据用户名，查询系统用户
	 */
	SysUserEntity queryByUsername(String username);
	
	/**
	 * 修改密码
	 */
	@Query(value = "update sys_user set password = :newPassword \n" +
			"\t\t\twhere user_id = :userId and password = :password",nativeQuery = true)
	int updatePassword(@Param("userId") Long userId,@Param("password") String password,@Param("newPassword") String newPassword);

}
